﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using landlord2lib;

namespace landlord2server
{
    public class GameTable
    {
        private int m_index;
        private int m_status;
        private GamePlayer[] m_players = new GamePlayer[4];
        private Card[] m_lordCards;
        private int? m_currentDir;
        private List<Card>[] m_playersCards;
        private int[] m_callLord;
        private CardGroup m_lastCardGroup;
        private int? m_lastDir;
        private int? m_lordDir;
        private int? m_point;

        public GameTable(int index)
        {
            m_index = index;
        }

        public int Index
        {
            get
            {
                return m_index;
            }
        }

        public int Status
        {
            get
            {
                return m_status;
            }
            set
            {
                m_status = value;
            }
        }

        public int PlayerCount
        {
            get
            {
                int count = 0;
                foreach (GamePlayer player in m_players)
                {
                    if (player != null) count++;
                }
                return count;
            }
        }

        public string[] Players
        {
            get
            {
                string[] result = new string[4];
                int i = 0;
                foreach (GamePlayer player in m_players)
                {
                    if (player != null) result[i] = player.Name;
                    i++;
                }
                return result;
            }
        }

        public bool CanStart
        {
            get
            {
                bool ret = true;
                foreach (GamePlayer player in m_players)
                {
                    if (player == null)
                    {
                        ret = false;
                        break;
                    }
                }
                return ret;
            }
        }

        public Card[] LordCards
        {
            get
            {
                return m_lordCards;
            }
            set
            {
                m_lordCards = value;
            }
        }

        public int? CurrentDir
        {
            get
            {
                return m_currentDir;
            }
            set
            {
                m_currentDir = value;
            }
        }

        public List<Card>[] PlayersCards
        {
            get
            {
                return m_playersCards;
            }
            set
            {
                m_playersCards = value;
            }
        }

        public int[] CallLord
        {
            get
            {
                return m_callLord;
            }
            set
            {
                m_callLord = value;
            }
        }

        public CardGroup LastCardGroup
        {
            get
            {
                return m_lastCardGroup;
            }
            set
            {
                m_lastCardGroup = value;
            }
        }

        public int? LastDir
        {
            get
            {
                return m_lastDir;
            }
            set
            {
                m_lastDir = value;
            }
        }

        public int? LordDir
        {
            get
            {
                return m_lordDir;
            }
            set
            {
                m_lordDir = value;
            }
        }

        public int? Point
        {
            get
            {
                return m_point;
            }
            set
            {
                m_point = value;
            }
        }

        public bool SetSeat(int seat, GamePlayer player)
        {
            if (m_players[seat] == null)
            {
                for (int i = 0; i < m_players.Length; i++)
                {
                    if (m_players[i] == player)
                    {
                        m_players[i] = null;
                        break;
                    }
                }
                m_players[seat] = player;
                player.Table = m_index;
                player.Seat = seat;
                return true;
            }
            return false;
        }

        public bool ClearSeat(GamePlayer player)
        {
            for (int i = 0; i < m_players.Length; i++)
            {
                if (m_players[i] == player)
                {
                    m_players[i] = null;
                    player.Seat = null;
                    player.Table = null;
                    return true;
                }
            }
            return false;
        }

        public GamePlayer[] GetPlayers()
        {
            return m_players;
        }

        public static DataSet CreateDataSet()
        {
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            dt.Columns.Add("Index", typeof(int));
            dt.Columns.Add("PlayerCount", typeof(int));
            dt.Columns.Add("Status", typeof(int));
            dt.Columns.Add("Player1", typeof(string));
            dt.Columns.Add("Player2", typeof(string));
            dt.Columns.Add("Player3", typeof(string));
            dt.Columns.Add("Player4", typeof(string));
            return ds;
        }
    }
}
